///Computes the "brightness" of a color
@use 'sass:math';

@function brightness($color) {
  @if type-of($color) == color {
    @return math.div(red($color) * 0.299 + green($color) * 0.587 + blue($color) * 0.114, 255) * 100%;
  }
  @else {
    @return unquote("brightness(#{$color})");
  }
}


///Select the more readable foreground color for a given background color.
@function contrast-color($color, $dark: $contrasted-dark, $light: $contrasted-light) {
  @if $color == null {
    @return null;
  }
  @else {
    $color-brightness: brightness($color);
    $dark-text-brightness: brightness($dark);
    $light-text-brightness: brightness($light);
    @return if(math.abs($color-brightness - $light-text-brightness) > math.abs($color-brightness - $dark-text-brightness), $light, $dark);
  }
}

@function add-z-index($key, $value) {
  @return map-merge($z-indexes, ($key: $value));
}

@function z-index($key) {
  @if map-has-key($z-indexes, $key) {
    @return map-get($z-indexes, $key);
  }

  @warn "Unknown key `#{$key}` in $z-indexes";
  @return null;
}

// _decimal.scss | MIT License | gist.github.com/terkel/4373420

// Round a number to specified digits.
//
// @param  {Number} $number A number to round
// @param  {Number} [$digits:0] Digits to output
// @param  {String} [$mode:round] (round|ceil|floor) How to round a number
// @return {Number} A rounded number
// @example
//     decimal-round(0.333)    => 0
//     decimal-round(0.333, 1) => 0.3
//     decimal-round(0.333, 2) => 0.33
//     decimal-round(0.666)    => 1
//     decimal-round(0.666, 1) => 0.7
//     decimal-round(0.666, 2) => 0.67
//
@function decimal-round ($number, $digits: 0, $mode: round) {
  $n: 1;
  // $number must be a number
  @if type-of($number) != number {
      @warn '#{ $number } is not a number.';
      @return $number;
  }
  // $digits must be a unitless number
  @if type-of($digits) != number {
      @warn '#{ $digits } is not a number.';
      @return $number;
  } @else if not unitless($digits) {
      @warn '#{ $digits } has a unit.';
      @return $number;
  }
  @for $i from 1 through $digits {
      $n: $n * 10;
  }
  @if $mode == round {
      @return math.div(round($number * $n), $n);
  } @else if $mode == ceil {
      @return math.div(ceil($number * $n), $n);
  } @else if $mode == floor {
      @return math.div(floor($number * $n), $n);
  } @else {
      @warn '#{ $mode } is undefined keyword.';
      @return $number;
  }
}

// Ceil a number to specified digits.
//
// @param  {Number} $number A number to round
// @param  {Number} [$digits:0] Digits to output
// @return {Number} A ceiled number
// @example
//     decimal-ceil(0.333)    => 1
//     decimal-ceil(0.333, 1) => 0.4
//     decimal-ceil(0.333, 2) => 0.34
//     decimal-ceil(0.666)    => 1
//     decimal-ceil(0.666, 1) => 0.7
//     decimal-ceil(0.666, 2) => 0.67
//
@function decimal-ceil ($number, $digits: 0) {
  @return decimal-round($number, $digits, ceil);
}

// Floor a number to specified digits.
//
// @param  {Number} $number A number to round
// @param  {Number} [$digits:0] Digits to output
// @return {Number} A floored number
// @example
//     decimal-floor(0.333)    => 0
//     decimal-floor(0.333, 1) => 0.3
//     decimal-floor(0.333, 2) => 0.33
//     decimal-floor(0.666)    => 0
//     decimal-floor(0.666, 1) => 0.6
//     decimal-floor(0.666, 2) => 0.66
//
@function decimal-floor ($number, $digits: 0) {
  @return decimal-round($number, $digits, floor);
}

@function sizzle-gradient($color) {
  $angle: 135deg;
  $startPos: 0%;
  $start: 0.3;
  $middlePos: 110px;
  $middle: 0.1;
  $endPos: 100%;
  $end: 0;

  @return transparent linear-gradient(#{$angle},
      #{rgba($color, $start)} #{$startPos},
      #{rgba($color, $middle)} #{$middlePos},
      #{rgba($color, $end)} #{$endPos}
  ) 0% 0% no-repeat padding-box;
}
